package com.example.administrator.balcklist.calllog;

import android.database.Cursor;
import android.provider.CallLog;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class CallRecord {
        String date;
        String formatted_number;
        String matched_number;
        String name = "";
        String type;
        String location;
        String duration;
        String isBlack = "";

        public CallRecord(Cursor cursor) {
                formatted_number = cursor.getString(0);
                matched_number = getNum(cursor.getString(1));
                String nametemp = cursor.getString(2);
                if (null != nametemp && nametemp.length() > 0) {
                        name = nametemp;
                }
                type = getCallType(cursor.getInt(3));
                date = formatDate(cursor.getLong(4));
                duration = formatDuration(cursor.getLong(5));
                location = cursor.getString(6);
        }

        private String getNum(String string) {
                if (null == string || string.length() == 0) {
                        return null;
                }
                return string.replace(" ","").replace("+86","");
        }

        private String getCallType(int anInt) {
                switch (anInt) {
                        case CallLog.Calls.INCOMING_TYPE:
                                return "呼入";
                        case CallLog.Calls.OUTGOING_TYPE:
                                return "呼出";
                        case CallLog.Calls.MISSED_TYPE:
                                return "未接";
                        default:
                                break;
                }
                return null;
        }


        @Override
        public String toString() {
            return date+type+":"+matched_number+" "+name+" 时长："+duration+" "+isBlack;
        }

        public String formatDate(long time) {
                DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.getDefault());
                return format.format(new Date(time));
        }

        public String formatDuration(long time) {
                long s = time % 60;
                long m = time / 60;
                long h = time / 60 / 60;
                StringBuilder sb = new StringBuilder();
                if (h > 0) {
                        sb.append(h).append("小时");
                }
                if (m > 0) {
                        sb.append(m).append("分");
                }
                sb.append(s).append("秒");
                return sb.toString();
        }


        public String getFormatted_number() {
                return formatted_number;
        }

        public void setFormatted_number(String formatted_number) {
                this.formatted_number = formatted_number;
        }

        public String getMatched_number() {
                return matched_number;
        }

        public void setMatched_number(String matched_number) {
                this.matched_number = matched_number;
        }

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public String getType() {
                return type;
        }

        public String getIsBlack() {
                return isBlack;
        }

        public void setIsBlack(String isBlack) {
                this.isBlack = isBlack;
        }

        public void setType(String type) {
                this.type = type;
        }

        public String getLocation() {
                return location;
        }

        public void setLocation(String location) {
                this.location = location;
        }

        public String getDate() {
                return date;
        }

        public void setDate(String date) {
                this.date = date;
        }

        public String getDuration() {
                return duration;
        }

        public void setDuration(String duration) {
                this.duration = duration;
        }
}